package com.zx.zxapibackend.config;

import com.zx.zxapicommon.constant.RabbitmqConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.zx.zxapicommon.constant.RabbitmqConstant.*;


/**
 * @author 张旭
 * @version 1.0
 */
@Configuration
public class SmsRabbitmqConfig {


    /**
     * 声明普通队列，并绑定死信交换机
     *
     * @return
     */
    @Bean
    public Queue smsQueue() {
        Map<String, Object> arguments = new HashMap<>();
        //声明死信队列和交换机消息，过期时间：1分钟
        arguments.put("x-dead-letter-exchange", SMS_EXCHANGE_NAME);
        arguments.put("x-dead-letter-routing-key", SMS_DELAY_EXCHANGE_ROUTING_KEY);
        arguments.put("x-message-ttl", 60000);
        return new Queue(SMS_QUEUE_NAME, true, false, false, arguments);
    }

    /**
     * 死信队列：消息重试三次后放入死信队列
     *
     * @return
     */
    @Bean
    public Queue smsDeadLetter() {
        return new Queue(SMS_DELAY_QUEUE_NAME, true, false, false);
    }

    /**
     * 主题交换机
     *
     * @return
     */
    @Bean
    public Exchange smsExchange() {
        return new TopicExchange(SMS_EXCHANGE_NAME, true, false);
    }


    /**
     * 交换机和普通队列绑定
     *
     * @return
     */
    @Bean
    public Binding smsBinding() {
        return new Binding(SMS_QUEUE_NAME, Binding.DestinationType.QUEUE, SMS_EXCHANGE_NAME, SMS_EXCHANGE_ROUTING_KEY, null);
    }

    /**
     * 交换机和死信队列绑定
     *
     * @return
     */
    @Bean
    public Binding smsDelayBinding() {
        return new Binding(SMS_DELAY_QUEUE_NAME, Binding.DestinationType.QUEUE, SMS_EXCHANGE_NAME, SMS_DELAY_EXCHANGE_ROUTING_KEY, null);
    }

}
